//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    bind: false,
    memberData: {
      avatar_link: "",
      birthday: "2001-01-01",
      calendar: 1,
      gender: 1,
      mobile: "",
      name: ""
    }
  },
  onShow() {
    if (!app.globalData.is_bind) {
      // 调用登录接口,get openID
      wx.showLoading({
        title: '更新用户信息',
        mask: true
      })
      wx.login({
        success: res => {
          app.$http('/token', {
            code: res.code
          }).then(response => {
            app.globalData.openid = response.token
            app.globalData.is_bind = response.is_bind
            this.setData({
              bind: response.is_bind
            })
          })
        }
      })
    } else {
      this.setData({
        bind: app.globalData.is_bind
      })
      app.$http('/profile').then(response => {
        this.setData({
          memberData: response
        })
      })
    }
  },
  changeInput(e) {
    if (e.target.id == 'gender' || e.target.id == 'calendar') {
      this.data.memberData[e.target.id] = Number(e.detail.value)
    } else {
      this.data.memberData[e.target.id] = e.detail.value
    }
    this.setData({
      memberData: this.data.memberData
    })
  },
  updateMember() {
    app.$http('/profile', this.data.memberData, 'put').then(response => {
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 2000
      })
    }, error => {})
  },
  openBind() {
    wx.navigateTo({
      url: '../member/bindPhone'
    })
  }
})
